/* Copyright (c) 2004-2022 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.core.result;

import cn.hutool.core.bean.copier.BeanCopier;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjUtil;
import info.peigen.hotpot.common.core.annotation.Comment;
import info.peigen.hotpot.common.core.result.facade.DtoAble;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * <b>(ListResult)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/4/9
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ListResult<T> extends ResultBase implements DtoAble {
    @ApiModelProperty(value = "返回对象集合", position = 1)
    @Comment("返回对象集合")
    private List<T> info;

    public void fromSuccess(List<T> infos) {
        setInfo(infos).setSuccessNotFee();
    }

    public void fromProcessing(List<T> infos) {
        setInfo(infos).markProcessing();
    }

    public ResultBase copyInfoTo(Object target, String... ignoreProperties) {
        if (target != null && !ObjUtil.isEmpty(this.getInfo())) {
            CopyOptions copyOptions = CopyOptions.create()
                    .setIgnoreNullValue(true)
                    .setIgnoreError(true)
                    .setIgnoreProperties(ignoreProperties);
            BeanCopier.create(this, target, copyOptions);
        }
        return this;
    }

}